package com.aisino.inveli.vinda.utils;

import com.aisino.inveli.vinda.entity.Customer;
import com.aisino.inveli.vinda.entity.GoodsDetail;
import com.aisino.inveli.vinda.entity.GoodsList;
import com.aisino.inveli.vinda.entity.TxtEntity;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leifeng
 * @Date 2017-11-14
 * txt文档操作类
 **/
public class TxtUtil {

    /**
     * 遍历当前文件夹下所有的txt文件
     * @param file
     * @return
     */
    public static List<TxtEntity> parsePath(File file) {
        List<TxtEntity> txtEntityList = new ArrayList<>();
        // 1.获取过滤后txt文件
        File[] files = file.listFiles(new TxtFileFilter());
        for (int i = 0; i < files.length; i++) {
            File temp = files[i];
            // 2.添加txt文件到list
            TxtEntity txtEntity = new TxtEntity();
            txtEntity.setTxtName(temp.getName());
            txtEntity.setTxtPath(temp.getPath());
            txtEntityList.add(txtEntity);
        }
        return  txtEntityList;
    }

    /**
     * 读取客户信息txt
     * @param file
     * @return
     */
    public static List<Customer> readCusTxt(File file) {
        ArrayList<Customer> cusList = new ArrayList<>();

        try {
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(file),"gbk"); // 建立一个输入流对象reader
            BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
            String line  = br.readLine();
            int i = 0;
            while (line != null) {
                line = br.readLine();
                if (i < 2){
                    i++;
                    continue;
                }
                if (line !=null){
                    String[] strings = line.split("~~");
                    if (strings!=null&& strings.length == 9 ){
                        Customer customer = new Customer();
                        customer.setCode(strings[0]);
                        customer.setCusName(strings[1]);
                        customer.setSimCode(strings[2]);
                        customer.setRegNumber(strings[3]);
                        customer.setAddAndTel(strings[4]);
                        customer.setBank(strings[5]);
                        customer.setEmail(strings[6]);
                        customer.setContent(strings[7]);
                        cusList.add(customer);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cusList;
    }
}
